//
//  VERemondContentTableViewCell.m
//  VEMoneyPorject
//
//  Created by BlueWind on 2018/9/10.
//  Copyright © 2018年 陈建. All rights reserved.
//

#import "VERemondContentTableViewCell.h"

@implementation VERemondContentTableViewCell

{
    
    
    UIView *_imageContentView; /**<图片 view*/

    UIButton *_whichSocietyButton; /**<属于哪一个社区*/
    
    UIView *_buttonContentView; /**<点赞，评论，转发按钮 view*/
    
    UILabel *_likeLabel; /**<点赞文字*/
    
}

/** 返回循环利用的cell */
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self= [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self loadDefaultSetting];
    }
    return self;
}

/** 加载默认UI元素和需要的数据 */
- (void)loadDefaultSetting {
    
    _summaryLabel = [UILabel new];
    [self.contentView addSubview:_summaryLabel];
    [_summaryLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(GAP8);
        make.left.equalTo(self.contentView).offset(GAP15);
        make.right.equalTo(self.contentView).offset(-GAP15);
    }];
    _summaryLabel.text = @"m setZoom:1.0];//open flash[liveRoom setFlash:YES]; 3.2.4 主播与观众的互动3.2.4.1 点赞向直播间发送点赞消息，建议在客户端上汇总点赞次数，1-2秒发送一次。示例代码如下 ";
    _summaryLabel.numberOfLines = 4;
    
//    _lookAll = [UIButton new];
//    [self.contentView addSubview:_lookAll];
//    [_lookAll mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self.summaryLabel.mas_bottom);
//        make.left.equalTo(self.summaryLabel);
//        make.height.mas_equalTo(30);
//    }];
//    [_lookAll setTitle:@"查看全文" forState:(UIControlStateNormal)];
//    [_lookAll setTitleColor:[UIColor blueColor] forState:(UIControlStateNormal)];
//    _lookAll.titleLabel.font = kFont(16);
    
    _imageContentView = [UIView new];
    [self.contentView addSubview:_imageContentView];
    [_imageContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self->_summaryLabel.mas_bottom).offset(GAP8);
        make.left.equalTo(self->_summaryLabel);
        make.right.equalTo(self->_summaryLabel);
        make.height.mas_equalTo((SCREEN_WIDTH - 2 *(GAP15 + GAP8))/3 + 16);
    }];
    _imageContentView.backgroundColor = [UIColor cyanColor];

    _whichSocietyButton = [UIButton new];
    [self.contentView addSubview:_whichSocietyButton];
    [_whichSocietyButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self->_imageContentView.mas_bottom).offset(GAP8);
        make.left.equalTo(self->_imageContentView);
        make.height.mas_equalTo(30);
    }];
    [_whichSocietyButton setTitle:@"行情交流群" forState:(UIControlStateNormal)];
    [_whichSocietyButton setContentEdgeInsets:UIEdgeInsetsMake(5, 10, 5, 10)];
    [_whichSocietyButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [_whichSocietyButton layerBorderWithWidth:0.5 borderColor:[UIColor orangeColor]];
    [_whichSocietyButton setLayerCornerRadius:5];
    [_whichSocietyButton addTarget:self action:@selector(enter) forControlEvents:(UIControlEventTouchUpInside)];
    
    _buttonContentView = [UIView new];
    [self.contentView addSubview:_buttonContentView];
    [_buttonContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self->_whichSocietyButton.mas_bottom).offset(GAP8);
        make.left.equalTo(self->_summaryLabel);
        make.height.mas_offset(30);
        make.width.mas_equalTo(120);
    }];
    
    NSArray *imageArray = @[@"点赞",@"留言",@"分享"];
    
    for (NSInteger index = 0; index < 3; index ++) {
        UIButton *button = [UIButton new];
        [_buttonContentView addSubview:button];
        button.tag = 10 + index;
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self->_buttonContentView).offset(index * 30 + GAP15 * (index + 1));
            make.centerY.equalTo(self->_buttonContentView);
            make.size.mas_equalTo(CGSizeMake(30, 30));
        }];
        [button setImage:[UIImage imageNamed:imageArray[index]] forState:(UIControlStateNormal)];
        [button addTarget:self action:@selector(caozuo:) forControlEvents:(UIControlEventTouchUpInside)];
    }
    
    _likeLabel = [UILabel new];
    [self.contentView addSubview:_likeLabel];
    [_likeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self->_buttonContentView.mas_bottom).offset(GAP8);
        make.left.equalTo(self->_buttonContentView);
        make.right.equalTo(self->_imageContentView);
    }];
    _likeLabel.numberOfLines = 0;
    
}


- (NSInteger)hideLabelLayoutHeight:(NSString *)content withTextFontSize:(CGFloat)mFontSize
{
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    
    paragraphStyle.lineSpacing = 10;  // 段落高度
    
    NSMutableAttributedString *attributes = [[NSMutableAttributedString alloc] initWithString:content];
    
    [attributes addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:mFontSize] range:NSMakeRange(0, content.length)];
    [attributes addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, content.length)];
    
    CGSize attSize = [attributes boundingRectWithSize:CGSizeMake(SCREEN_WIDTH - 30, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil].size;
    
    return attSize.height;
}

//
//- (void)setIsDetail:(BOOL)isDetail {
//    _isDetail = isDetail;
//    if (isDetail == YES) {
//        [_imageContentView mas_remakeConstraints:^(MASConstraintMaker *make) {
//            make.top.equalTo(self->_summaryLabel.mas_bottom).offset(GAP8);
//            make.left.equalTo(self->_summaryLabel);
//            make.right.equalTo(self->_summaryLabel);
//            make.height.mas_equalTo((SCREEN_WIDTH - 2 *(GAP15 + GAP8))/3 + 16);
//        }];
//        _summaryLabel.numberOfLines = 0;
//    }
//}

//- (void)setIsDongtai:(BOOL)isDongtai {
//    _isDongtai = isDongtai;
//    if (isDongtai == YES) {
//        _whichSocietyButton.hidden = YES;
//        [_buttonContentView mas_remakeConstraints:^(MASConstraintMaker *make) {
//            make.top.equalTo(self->_imageContentView.mas_bottom).offset(GAP8);
//            make.left.equalTo(self->_summaryLabel);
//            make.height.mas_offset(30);
//            make.width.mas_equalTo(120);
//        }];
//    }
//
//}

- (void)setModel:(VERemondModel *)model {
    _model = model;
    
    CGFloat h = [model.DYNAMICTEXT heigthWithWeight:(SCREEN_WIDTH - 30) andFont:16 andLineSpacing:2 andWordSpacing:1];
    
    if (h > 100) {
        if (_isDetail == NO) {
            _lookAll.hidden = NO;
        } else {
            _lookAll.hidden = YES;
        }

    } else {
        _lookAll.hidden = YES;
//        [_imageContentView mas_remakeConstraints:^(MASConstraintMaker *make) {
//            make.top.equalTo(self->_summaryLabel.mas_bottom).offset(GAP8);
//            make.left.equalTo(self->_summaryLabel);
//            make.right.equalTo(self->_summaryLabel);
//            make.height.mas_equalTo((SCREEN_WIDTH - 2 *(GAP15 + GAP8))/3 + 16);
//        }];
    }
    
    _summaryLabel.text = model.DYNAMICTEXT;
    
    
    
    if (model.DYNAMICPHOTO.length == 0) {
        _imageContentView.hidden = YES;
        if (_lookAll.hidden == NO) {
            [_whichSocietyButton mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self->_summaryLabel.mas_bottom).offset(GAP8);
                make.left.equalTo(self->_summaryLabel);
                make.height.mas_equalTo(30);
            }];
        } else {
            [_whichSocietyButton mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self->_summaryLabel.mas_bottom).offset(GAP8);
                make.left.equalTo(self->_summaryLabel);
                make.height.mas_equalTo(30);
            }];
        }
    } else {
        _imageContentView.hidden = NO;
        [_imageContentView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self->_summaryLabel.mas_bottom).offset(GAP8);
            make.left.equalTo(self->_summaryLabel);
            make.right.equalTo(self->_summaryLabel);
            make.height.mas_equalTo((SCREEN_WIDTH - 2 *(GAP15 + GAP8))/3 + 16);
        }];
        NSArray *imageArray = [model.DYNAMICPHOTO componentsSeparatedByString:@","];
        for (id obj in self.contentView.subviews) {
            if ([obj isKindOfClass:[UIView class]]) {
                [obj removeFromSuperview];
            }
        }
        
        CGFloat PIC_WIDTH = (SCREEN_WIDTH-6 *GAP8)/3;
        CGFloat PIC_HEIGHT = (SCREEN_WIDTH-6 *GAP8)/3;
        NSInteger COL_COUNT = 3;
        // 间距
        CGFloat margin = 8;
        
        for (int i = 0; i < imageArray.count; i++) {
            if (i == 9) {
                return;
            }
            // 图片所在行
            NSInteger row = i / COL_COUNT;
            // 图片所在列
            NSInteger col = i % COL_COUNT;
            // PointX
            CGFloat picX = GAP8 * 2 + (PIC_WIDTH + margin) * col;
            // PointY
            CGFloat picY = (PIC_HEIGHT + margin) * row;
            
            UIView * bgView = [[UIView alloc] init];
            bgView.frame = CGRectMake(picX, picY, PIC_WIDTH, PIC_HEIGHT);
            [self.contentView addSubview:bgView];
            
            UIImageView * imageView = [[UIImageView alloc] init];
            imageView.contentMode = UIViewContentModeScaleAspectFill;
            imageView.clipsToBounds = YES;
            imageView.tag = 20 + i;
            imageView.frame = CGRectMake(0, 0, PIC_WIDTH, PIC_HEIGHT);
            [imageView setImageWithUrlString:imageArray[i] palceHolderImage:[UIImage imageNamed:@"First_PlaceHolder"]];
            [bgView addSubview:imageView];
            
            imageView.userInteractionEnabled = YES;
//            [imageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickAddImageView:)]];
        }
    }
[_whichSocietyButton setTitle:model.COMMUNITYNAME forState:(UIControlStateNormal)];
    
    if (model.ZANNAME.length == 0) {
        _likeLabel.hidden = YES;
    } else {
        _likeLabel.hidden = NO;
        //创建富文本
        NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:model.ZANNAME];
        //NSTextAttachment可以将要插入的图片作为特殊字符处理
        NSTextAttachment *attch = [[NSTextAttachment alloc] init];
        //定义图片内容及位置和大小
        attch.image = [UIImage imageNamed:@"超级赞"];
        attch.bounds = CGRectMake(0, 0, 14, 14);
        //创建带有图片的富文本
        NSAttributedString *string = [NSAttributedString attributedStringWithAttachment:attch];
        //将图片放在第一位
        [attri insertAttributedString:string atIndex:0];
        //用label的attributedText属性来使用富文本
        _likeLabel.attributedText = attri;
    }
    
    
}

- (void)caozuo:(UIButton *)button {
    if (button.tag == 10) { //点赞
        
        NSString *userId = [[CJUserInfoManager sharedManager] currentUserInfo].USERID;
        
        NSDictionary *dic = @{
                              @"Userid": userId,
                              @"Dynamicid": _model.DYNAMICID,
                              };
        NSString *url = [NSString stringWithFormat:@"%@/VEService/GetZAN.do",kPublicInterfaceAPI];
        CJBaseRequest *request = [CJBaseRequest cj_requestWithUrl:url isPost:YES withParameters:dic];
        [request cj_sendRequestWithCompletion:^(id response, BOOL success, NSString *message) {
            
            [CJUIKit showSuccessWithStatus:message];
            
        }];
        
    } else if (button.tag == 11) {
        if (_sendMessage) {
            _sendMessage();
        }
        
    } else if (button.tag == 12) {
        
        if (_share) {
            _share();
        }
        
    }
}

- (void)enter {
    if (_enterAssion) {
        _enterAssion();
    }
}

@end
